package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    /*@GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询员工信息,{} , {}",page,pageSize);
       PageBean pageBean =  empService.page(page , pageSize);
        return Result.success(pageBean);
    }*/
    /**
     * 条件分页查询
     */
    @GetMapping
    public Result page(EmpQueryParam queryParam) {
        log.info("分页查询： {}", queryParam);
        PageBean pageBean = empService.page(queryParam);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工: {}" , emp);
        empService.add(emp);
        return Result.success();
    }
    /**
     * 新增 删除和批量删除的功能
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工：{}",ids);
        empService.delete(ids);
        return Result.success();
    }
    /**
     * 点击编辑回显出所有信息，包含两个表一个员工emp表一个员工经历empExpr表
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工的详细信息");
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }
    /**
     * 添加修改员工的功能
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectAll(){
        //调用empServise
           List<Emp> empList =empService.selectAll();
        return Result.success(empList);
    }

}
